package com.henry.db.migrate.main;

import com.henry.db.migrate.factory.DbFactory;
import com.henry.db.migrate.model.TableModel;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.log4j.Logger;

import java.sql.Connection;

public class TestThread extends Thread{
    private static final Logger logger = Logger.getLogger(TestThread.class);
    private final TableModel tableModel;

    public TestThread(TableModel tableModel) {
        this.tableModel=tableModel;
    }

    @Override
    public void run() {
        logger.info("["+tableModel.getTablename()+"]的线程正在执行");
        Connection srcConn = DbFactory.getConnection("src");
        try {
            QueryRunner runner = new QueryRunner();
            srcConn.setAutoCommit(false);
            runner.batch(srcConn, tableModel.getInsertSql(), tableModel.genParams());
            srcConn.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            DbFactory.rollback(srcConn);
        } finally {
            DbFactory.close(srcConn);
        }
        logger.info("["+tableModel.getTablename()+"]的线程执行完毕");
    }
}
